//
//  ByNetCodeApi.m
//  beibei_ios
//
//  Created by pro on 16/5/5.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByNetCodeApi.h"

@implementation ByNetCodeApi

/** 发送手机验证码 */
+ (void)getCode:(ByNetParamsModel *)model
        success:(void(^)(ByNetBaseResult *result))success
        failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_SecurityCode_send";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.mobile forKey:@"mobile"];
    [params setObject:model.type forKey:@"type"];
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           // 若返回数据为字典类型则抛出异常
           NSAssert([responseObject isKindOfClass:[NSString class]], @"请求验证码 返回数据是字符串类型的，请检查");
           
           CodeLog(@"******** %@ 请求成功: %@", apiType, responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = responseObject;
           result.data = responseObject; // 验证码（6位字符串）
           result.notify_id = notifyID;

           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           CodeLog(@"******** %@ 请求失败: %@", apiType, error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert alertError:result.msg];
           }
       }];

}

/** 验证验证码 */
//+ (void)checkCode:(ByNetCodeModel *)model
//          success:(void(^)(ByNetBaseResult *result))success
//          failure:(void(^)(ByNetBaseResult *result))failure {
//    
//}
@end
